package com.tle.eventexample;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.tle.libs.CONSTANTS;
import com.tle.libs.CONSTANTS.FONT_PATHS;
import com.tle.libs.CONSTANTS.MARGINS;
import com.tle.libs.CONSTANTS.TEXT_SIZES;
import com.tle.libs.UIUtilities;

public class Converter extends BaseActivity 
{
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Properties //
	////////////////////////////////////////////////////////////////////////////////////////////////////	
	protected LinearLayout m_layoutMain;
	protected LayoutParams marginLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
	
	protected RadioButton m_rdoConvertYear;
	protected RadioButton m_rdoConvertTemperature;
	
	protected Button m_btnConverter;
	protected Button m_btnComeBack;
	
	protected enum LAYOUT_INDEX
	{
		RADIO_CONVERTER (0),
		LAYOUT_CONVERT_YEAR (1),
		LAYOUT_CONVERT_TEMPERATURE (1),
		BUTTON_COME_BACK (2);
		
		private int m_nIndex;
		
		private LAYOUT_INDEX (int nIndex)
		{
			m_nIndex = nIndex;
		}
		
		public int getValue ()
		{
			return m_nIndex;
		}
		
	}
	
	
	
	// Convert Year
	protected LinearLayout m_layoutConvertYear;
	
	protected TextView m_lblSolarYear;
	protected EditText m_txtSolarYear;
	
	protected TextView m_lblLunarYear;
	protected EditText m_txtLunarYear;
	
	protected String m_arrCan[] = {"Canh", "Tân", "Nhâm", "Quý", "Giáp", "Ất", "Bính", "Đinh", "Mậu", "Kỷ"};
	protected String m_arrChi[] = {"Thân", "Dậu", "Tuất", "Hợi", "Tý", "Sửu", "Dần", "Mẹo", "Thìn", "Tỵ", "Ngọ", "Mùi"};
	
	
	
	// Convert Temperature
	protected LinearLayout m_layoutConvertTemperature;
	
	protected TextView m_lblCelsius;
	protected EditText m_txtCelsius;
	
	protected TextView m_lblFahrenheit;
	protected EditText m_txtFahrenheit;
	
	protected RadioButton m_rdoToC;
	protected RadioButton m_rdoToF;
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
																						// Properties //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	
	
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Constructor and Support Method //
	////////////////////////////////////////////////////////////////////////////////////////////////////
		
	protected void setActivityTitle() 
	{
		this.setTitle(R.string.title_YearConverter); // this code is not effect because it call before buildRadioConverter
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		this.initGUI();
		
		UIUtilities.setUIStandard(m_rdoConvertTemperature, FONT_PATHS.PLAINT, TEXT_SIZES.NORMAL);
		UIUtilities.setUIStandard(m_rdoConvertYear, FONT_PATHS.PLAINT, TEXT_SIZES.NORMAL);
		UIUtilities.setUIStandard(m_btnComeBack, FONT_PATHS.PLAINT, TEXT_SIZES.NORMAL);
		
		this.registerEvent();
		
		this.initProperties();
	}
	
		
	
	
	
	protected void initGUI ()
	{		
		m_layoutConvertYear = this.buildConvertYearLayout();
		m_layoutConvertTemperature = this.buildConvertTemperatureLayout();
		
		UIUtilities.setUIStandard(m_layoutConvertTemperature, FONT_PATHS.PLAINT, TEXT_SIZES.NORMAL);
		UIUtilities.setUIStandard(m_layoutConvertYear, FONT_PATHS.PLAINT, TEXT_SIZES.NORMAL);
		
		
		
		m_layoutMain = new LinearLayout(this);
		m_layoutMain.setOrientation(LinearLayout.VERTICAL);
		this.setContentView(m_layoutMain);
		
		marginLayoutParams.leftMargin = marginLayoutParams.rightMargin = CONSTANTS.MARGINS.M5.getValue();
		marginLayoutParams.gravity = Gravity.CENTER_HORIZONTAL;
		
		
		m_layoutMain.addView(this.buildRadioConverter() , LAYOUT_INDEX.RADIO_CONVERTER.getValue() , marginLayoutParams);
			
		
		
		m_layoutMain.addView(m_layoutConvertYear , LAYOUT_INDEX.LAYOUT_CONVERT_YEAR.getValue() , marginLayoutParams);
		
		
		
		m_btnComeBack = new Button(this);
		m_btnComeBack.setText(R.string.const_btnComeBack);
		m_layoutMain.addView(m_btnComeBack , LAYOUT_INDEX.BUTTON_COME_BACK.getValue() , marginLayoutParams);
	}	
	
	private HorizontalScrollView buildRadioConverter ()
	{
		m_rdoConvertYear = new RadioButton(this);
		m_rdoConvertYear.setText(R.string.title_YearConverter);
		
		m_rdoConvertTemperature = new RadioButton(this);
		m_rdoConvertTemperature.setText(R.string.title_TemperatureConverter);
		
		ArrayList<RadioButton> arrRdoButtons = new ArrayList<>();
		arrRdoButtons.add(m_rdoConvertYear);
		arrRdoButtons.add(m_rdoConvertTemperature);
		
		RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT , 1);
		layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
		
		this.setTitle(R.string.title_YearConverter);
		
		return UIUtilities.buildRadioButtons(arrRdoButtons, layoutParams, 0);
	}
	
	private TableLayout buildConvertYearLayout ()
	{
		m_lblSolarYear = new TextView(this);
		m_lblSolarYear.setText(R.string.Converter_lblSolarYear);
		m_lblSolarYear.setGravity(Gravity.RIGHT);
		
		m_txtSolarYear = new EditText(this);
		m_txtSolarYear.setInputType(InputType.TYPE_CLASS_NUMBER);
		m_txtSolarYear.requestFocus();
		
		
		
		m_btnConverter = new Button(this);
		m_btnConverter.setText(R.string.Converter_btnConverter);
		
		
		
		m_lblLunarYear = new TextView(this);
		m_lblLunarYear.setText(R.string.Converter_lblLunarYear);
		m_lblLunarYear.setGravity(Gravity.RIGHT);
		
		m_txtLunarYear = new EditText(this);
		m_txtLunarYear.setInputType(InputType.TYPE_CLASS_NUMBER);
		m_txtLunarYear.setTextIsSelectable(true);
		m_txtLunarYear.setKeyListener(null);
		
		
		
		TableLayout mainTableLayout = new TableLayout(this);
		
		TableRow.LayoutParams col0Params = new TableRow.LayoutParams();
		col0Params.column = 0;
		col0Params.setMargins(MARGINS.M5.getValue(), 0, MARGINS.M5.getValue(), 0);
		
		TableRow.LayoutParams col1Params = new TableRow.LayoutParams();
		col1Params.weight = 1.0f;
		col1Params.column = 1;
		col0Params.setMargins(0, 0, MARGINS.M5.getValue(), 0);
		
		
		
		TableRow row = new TableRow(this);
		row.addView(m_lblSolarYear, col0Params);
		row.addView(m_txtSolarYear, col1Params);
		mainTableLayout.addView(row);
		
		
		
		row = new TableRow(this);
		row.addView(m_btnConverter, col1Params);
		mainTableLayout.addView(row);
		
		
		row = new TableRow(this);
		row.addView(m_lblLunarYear, col0Params);
		row.addView(m_txtLunarYear, col1Params);
		mainTableLayout.addView(row);
		
		return mainTableLayout;
	}

	private LinearLayout buildConvertTemperatureLayout()
	{
		LinearLayout resLayout = new LinearLayout(this);
		resLayout.setOrientation(LinearLayout.VERTICAL);
		
		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		
		
		
		m_lblCelsius = new TextView(this);
		m_lblCelsius.setText(R.string.Converter_lblCelsius);
		resLayout.addView(m_lblCelsius , layoutParams);
		
		m_txtCelsius = new EditText(this);
		m_txtCelsius.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
		m_txtCelsius.setEnabled(false);
		resLayout.addView(m_txtCelsius , layoutParams);
		
		
		
		m_lblFahrenheit = new TextView(this);
		m_lblFahrenheit.setText(R.string.Converter_lblFahrenheit);
		resLayout.addView(m_lblFahrenheit , layoutParams);
		
		m_txtFahrenheit = new EditText(this);
		m_txtFahrenheit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
		m_txtFahrenheit.requestFocus();
		resLayout.addView(m_txtFahrenheit , layoutParams);
		
		
		
		m_rdoToC = new RadioButton(this);
		m_rdoToC.setText(R.string.Converter_rdoToC);
		
		m_rdoToF = new RadioButton(this);
		m_rdoToF.setText(R.string.Converter_rdoToF);
		
		RadioGroup.LayoutParams rdoLayoutParams = new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
		ArrayList<RadioButton> arrRdoBtns = new ArrayList<>();
		arrRdoBtns.add(m_rdoToC);
		arrRdoBtns.add(m_rdoToF);
		resLayout.addView( UIUtilities.buildRadioButtons(arrRdoBtns, rdoLayoutParams, 0) , layoutParams);
		
		return resLayout;
	}
	
	
	
	
	
	protected void initProperties ()
	{
		UIUtilities.showSoftKeyboard(this, m_txtSolarYear);
	}
	
	
	
	
	
	protected void registerEvent ()
	{
		ButtonClickEvent buttonEvent = new ButtonClickEvent();
		m_btnComeBack.setOnClickListener(buttonEvent);
		m_btnConverter.setOnClickListener(buttonEvent);
		
		
		
		EditTextClickEvent editTextClickEvent = new EditTextClickEvent();
		m_txtLunarYear.setOnClickListener(editTextClickEvent);
		
		
		
		RadioButtonEvent radioButtonEvent = new RadioButtonEvent();
		m_rdoConvertTemperature.setOnCheckedChangeListener(radioButtonEvent);
		m_rdoConvertYear.setOnCheckedChangeListener(radioButtonEvent);

		m_rdoToC.setOnCheckedChangeListener(radioButtonEvent);
		m_rdoToF.setOnCheckedChangeListener(radioButtonEvent);
		
		
		
		KeyReleaseEvent keyReleaseEvent = new KeyReleaseEvent();
		m_txtCelsius.setOnKeyListener(keyReleaseEvent);
		m_txtFahrenheit.setOnKeyListener(keyReleaseEvent);
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
																	// Constructor and Support Method //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	
	
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Support class and methods //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	private class ButtonClickEvent implements OnClickListener
	{		
		@Override
		public void onClick(View v) 
		{
			// TODO Auto-generated method stub
			if(v == m_btnComeBack)
			{		
				Toast toast = Toast.makeText(Converter.this, R.string.Converter_toastComeBack, Toast.LENGTH_LONG);
				toast.show();
				
				finish();
				
			}// m_btnComeBack
			
			else if (v == m_btnConverter)
			{
				convertYear();
				UIUtilities.hideSoftKeyboard(Converter.this);
			}// m_btnConverter
			
		}
	}
	
	
	
	private class EditTextClickEvent implements OnClickListener
	{
		@Override
		public void onClick(View v) 
		{
			// TODO Auto-generated method stub
			if (v == m_txtLunarYear)
			{
				m_txtLunarYear.setSelection(0 , m_txtLunarYear.getText().length());
			}// end m_txtLunarYear
			
		}
	}
	
	
	
	private class RadioButtonEvent implements OnCheckedChangeListener
	{
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
		{
			// TODO Auto-generated method stub
						
			if (buttonView == m_rdoConvertTemperature && isChecked == true)
			{
				m_layoutMain.removeViewAt(LAYOUT_INDEX.LAYOUT_CONVERT_YEAR.getValue());
				m_layoutMain.addView(m_layoutConvertTemperature, LAYOUT_INDEX.LAYOUT_CONVERT_TEMPERATURE.getValue(), marginLayoutParams);
				
				resetConvertTemperature();
				
			}// end m_rdoConvertTemperature
			
			else if (buttonView == m_rdoConvertYear && isChecked == true)
			{
				m_layoutMain.removeViewAt(LAYOUT_INDEX.LAYOUT_CONVERT_TEMPERATURE.getValue());
				m_layoutMain.addView(m_layoutConvertYear, LAYOUT_INDEX.LAYOUT_CONVERT_YEAR.getValue(), marginLayoutParams);
				
				resetConvertYear();

			}// end m_rdoConvertYear
			
			else if (buttonView == m_rdoToC && isChecked == true)
			{
				m_txtCelsius.setEnabled(false);
				m_txtFahrenheit.setEnabled(true);
				
				m_txtCelsius.setText("");
				m_txtFahrenheit.setText("");
				
				m_txtFahrenheit.requestFocus();
				
				UIUtilities.showSoftKeyboard(Converter.this, m_txtFahrenheit);
				
			}// end m_rdoToC
			
			else if (buttonView == m_rdoToF && isChecked == true)
			{	
				m_txtCelsius.setEnabled(true);
				m_txtFahrenheit.setEnabled(false);
				
				m_txtCelsius.setText("");
				m_txtFahrenheit.setText("");
				
				m_txtCelsius.requestFocus();
				
				UIUtilities.showSoftKeyboard(Converter.this, m_txtCelsius);
				
			}// end m_rdoToF
			
		}
	}
	
	private void resetConvertTemperature ()
	{
//		m_rdoToC.setSelected(true);
		m_rdoToC.setChecked(true);
		m_txtCelsius.setText("");
		m_txtFahrenheit.setText("");
		
		m_txtFahrenheit.requestFocus();
		
		this.setTitle(R.string.title_TemperatureConverter);
		
		UIUtilities.showSoftKeyboard(this, m_txtFahrenheit);
	}
	
	private void resetConvertYear ()
	{
		m_txtLunarYear.setText("");
		m_txtSolarYear.setText("");
		
		m_txtSolarYear.requestFocus();
		
		this.setTitle(R.string.title_YearConverter);
		
		UIUtilities.showSoftKeyboard(this, m_txtSolarYear);
	}
	
	
	
	private class KeyReleaseEvent implements OnKeyListener
	{
		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) 
		{
			// TODO Auto-generated method stub
			
			if (event.getAction() == KeyEvent.ACTION_UP)
			{
				if(v == m_txtCelsius)
				{
					convertToFahrenheit();
					
					return true;
				}// end m_txtCelsius
				
				else if (v == m_txtFahrenheit)
				{
					convertToCelsius();
					
					return true;
				}// end m_txtFahrenheit
				
			}
			
			return false;
		}
	}
	
	private void convertYear()
	{
		if (m_txtSolarYear.getText().toString().isEmpty() == true)
			return;
		
		int nSolarYear = Integer.parseInt(m_txtSolarYear.getText().toString());
		
		int nCanIndex = nSolarYear % m_arrCan.length;
		String sCan = m_arrCan[nCanIndex];
		
		int nChiIndex = nSolarYear % m_arrChi.length;
		String sChi = m_arrChi[nChiIndex];
		
		m_txtLunarYear.setText(sCan + " " + sChi);
	}
	
	private void convertToCelsius ()
	{
		boolean bFahrenheitEmpty = m_txtFahrenheit.getText().toString().isEmpty();
		boolean bIsDot = m_txtFahrenheit.getText().toString().equals(".");
		
		if (bFahrenheitEmpty == true || bIsDot == true)
			return;
		
		DecimalFormat decimalFormat = new DecimalFormat();
		decimalFormat.setMinimumFractionDigits(0);
		decimalFormat.setMaximumFractionDigits(2);
		
		float ftFahrenheit = Float.parseFloat(m_txtFahrenheit.getText().toString());
		float ftCelsius = (ftFahrenheit - 32)*5/9;
		
		m_txtCelsius.setText( decimalFormat.format(ftCelsius) );
	}
	
	private void convertToFahrenheit()
	{
		boolean bCelsiusEmpty = m_txtCelsius.getText().toString().isEmpty();
		boolean bIsDot = m_txtCelsius.getText().toString().equals(".");
		
		if (bCelsiusEmpty == true || bIsDot == true)
			return;
		
		DecimalFormat decimalFormat = new DecimalFormat();
		decimalFormat.setMinimumFractionDigits(0);
		decimalFormat.setMaximumFractionDigits(2);
		
		float ftCelsius = Float.parseFloat(m_txtCelsius.getText().toString());
		float ftFahrenheit = ftCelsius*9/5 + 32;
		
		m_txtFahrenheit.setText( decimalFormat.format(ftFahrenheit) );
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
																		 // Support class and methods //
	////////////////////////////////////////////////////////////////////////////////////////////////////
}